#pragma once

#include <QObject>
#include <QTimer>
#include <QTcpSocket>
#include <QMutex>
#include <QObject>
#include <QThread>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>

#include "commonDefine.h"

namespace CommonComponet
{

	class SerialPortClient : public QObject
	{
		Q_OBJECT

	public:
		SerialPortClient(QSharedPointer<CommonComponet::SerialInfo> serical_info, DEVICE_CONN_TYPE comtype, QObject* parent = NULL);
		~SerialPortClient();

	signals:
		void sig_connection_state(int state);
		void sig_receiveDataFromServer(QString);

	public slots:
		void Started();

	public slots:
		void slot_Reconnect(const QString& comport);
		void slot_send_message(QString message);
		void slot_async_timeout_send_message(QString message, int write_timeout, int read_timeout);
		QString slot_send_and_wait_message(QString message, int write_timeout, int read_timeout); //
		void recv_device_data();
		void readPortDataTimer();

	private:
		QSerialPort* m_port = nullptr;
		QSharedPointer<CommonComponet::SerialInfo> m_serical_info;
		DEVICE_CONN_TYPE  m_ComType;

		bool m_working = false;
		QString m_com_data;
		int m_timer_out = 0;

		QTimer* m_ASYNC_timer = nullptr;

	private:
		DeviceConnState m_connState = DeviceConnState::UnconnectedState;
	};
}
